<?php

/**
 * 快递信息
 * 
 * 版权所有(C) 乐印网
 * @author sujinyue
 * @date 2016-02-04
 * @version 1.1
 */
Class Kuaidi100
{

    /**
     * 需要用到htmlApi的快递
     * 
     * @author wechan
     */
    public $htmlApi = array(
        'yuantong',
        'shentong',
        'zhongtong',
        'shunfeng',
        'yunda',
    );
    /**
     *  获取快递100返回信息
     *
     * @param $cod_kuaidi 快递公司代号
     * @param $nu_kuaidi  快递单号
     * @author sujinyue
     * @date 2016-04-26
     */
    static function kuaiDiInfo($cod_kuaidi,$nu_kuaidi){
        $result = self::getcontent("http://www.kuaidi100.com/query?id=1&type={$cod_kuaidi}&postid={$nu_kuaidi}");
        $result = json_decode($result);
        $data = self::json_array($result);
        return $data;
    }

    /**
     * 获取网页内容
     *
     * @param $url
     * @return mixed|string
     * @throws Exception
     * @author luochangjiang
     * @date 2017-06-01
     */
    private function getcontent($url){
        if(function_exists("file_get_contents")){
            $file_contents = file_get_contents($url);
        }else{
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $file_contents = curl_exec($ch);
            $curl_errno = (int)curl_errno($ch);
            $errorinfo = curl_getinfo($ch);
            if ($curl_errno !== 0 || $errorinfo['http_code'] != 200) {
               /* $log = Log::factory(NT_LOGS_PATH . 'wuliuLog/'); // 记录日志
                $log->write2('getwuliu_err', array(
                    msg => json_encode($errorinfo)
                ));*/
            }
            curl_close($ch);
        }
        return $file_contents;
    }

    /*
	* 解析object成数组的方法
	* @param $json 输入的object数组
	* return $data 数组
    * @author luochangjiang
    * @date 2017-06-01
	*/
    private function json_array($json){
        if($json){
            foreach ((array)$json as $k=>$v){
                $data[$k] = !is_string($v)?self::json_array($v):$v;
            }
            return $data;
        }
    }

    /**
     * 获取htmlApi快递100的返回信息
     * 
     * @param $codKuaidi 快递公司代号
     * @param $nuKuaidi  快递单号
     * @author wechan
     * @date 2016年04月29日
     */
    public function htmlApiExpressInfo($codKuaidi,$nuKuaidi){
        $express100 = C("kuaidi100");
        var_dump($express100);
        $htmlApiUrl = $express100['htmlApiUrl'];
        $key = $express100['key'];
        $url = $htmlApiUrl.'?key='.$key.'&com='.$codKuaidi.'&nu='.$nuKuaidi;
        $urlResult = $this->getUrlContents($url);
        return $urlResult;
    }

    /**
     * 读取URL的内容
     *
     * @author wechan
     * @param string $url
     * @param array $data
     */
    public  function getUrlContents($url, $data=array())
    {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        !empty($data) && curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        curl_exec($ch);
        $result=ob_get_contents();
        ob_clean();
        curl_close($ch);
        return $result;
    }
}

?>